package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.SysSupplier;
import com.ruoyi.system.mapper.SysSupplierMapper;
import com.ruoyi.system.service.ISysSupplierService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 供应商信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-07-19
 */
@Service
public class SysSupplierServiceImpl implements ISysSupplierService {

    private static final Logger log = LoggerFactory.getLogger(SysSupplierServiceImpl.class);

    @Autowired
    private SysSupplierMapper sysSupplierMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询供应商信息
     * 
     * @param supplierId 供应商信息主键
     * @return 供应商信息
     */
    @Override
    public SysSupplier selectSysSupplierBySupplierId(Long supplierId)
    {
        return sysSupplierMapper.selectSysSupplierBySupplierId(supplierId);
    }

    /**
     * 查询供应商信息列表
     * 
     * @param sysSupplier 供应商信息
     * @return 供应商信息
     */
    @Override
    public List<SysSupplier> selectSysSupplierList(SysSupplier sysSupplier)
    {
        return sysSupplierMapper.selectSysSupplierList(sysSupplier);
    }

    /**
     * 新增供应商信息
     * 
     * @param sysSupplier 供应商信息
     * @return 结果
     */
    @Override
    public int insertSysSupplier(SysSupplier sysSupplier)
    {
        sysSupplier.setCreateTime(DateUtils.getNowDate());
        return sysSupplierMapper.insertSysSupplier(sysSupplier);
    }

    /**
     * 修改供应商信息
     * 
     * @param sysSupplier 供应商信息
     * @return 结果
     */
    @Override
    public int updateSysSupplier(SysSupplier sysSupplier)
    {
        sysSupplier.setUpdateTime(DateUtils.getNowDate());
        return sysSupplierMapper.updateSysSupplier(sysSupplier);
    }

    /**
     * 批量删除供应商信息
     * 
     * @param supplierIds 需要删除的供应商信息主键
     * @return 结果
     */
    @Override
    public int deleteSysSupplierBySupplierIds(String supplierIds)
    {
        return sysSupplierMapper.deleteSysSupplierBySupplierIds(Convert.toStrArray(supplierIds));
    }

    /**
     * 删除供应商信息信息
     * 
     * @param supplierId 供应商信息主键
     * @return 结果
     */
    @Override
    public int deleteSysSupplierBySupplierId(Long supplierId)
    {
        return sysSupplierMapper.deleteSysSupplierBySupplierId(supplierId);
    }

    /**
     * 数据导入
     *
     * @param list 导入的数据列表
     * @param isUpdateSupport 是否更新已有数据
     * @param operName 操作人的名字
     * @return 结果
     */
    @Override
    public String importSupplier(List<SysSupplier> list, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(list) || list.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SysSupplier obj : list)
        {
            try
            {
                // 验证是否存在这个公司
                SysSupplier c = sysSupplierMapper.selectSysSupplierBySupplierId(obj.getSupplierId());
                if (StringUtils.isNull(c))
                {
                    BeanValidators.validateWithException(validator, obj);
                    obj.setCreateBy(operName);
                    this.insertSysSupplier(obj);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、厂商 " +obj.getSupplierName() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    BeanValidators.validateWithException(validator, obj);
                    obj.setUpdateBy(operName);
                    this.updateSysSupplier(obj);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、厂商 " +obj.getSupplierName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、厂商 " +obj.getSupplierName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、厂商 " +obj.getSupplierName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
